package com.tao.com.xiaowen.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.tao.com.xiaowen.model.Picture;
import com.tao.com.xiaowen.R;

import java.util.List;

/**
 * Created by Administrator on 2016/8/20.
 */
public class PictureFragmentAdapter extends XRecyclerView.Adapter<PictureFragmentAdapter.PictureViewHolder> {
    private Context context;
    private List<Picture> pictureList;
    private LayoutInflater inflater;

    private OnItemClickListener onItemClickListener;

    public PictureFragmentAdapter(Context context, List<Picture> pictureList) {
        this.context = context;
        this.pictureList = pictureList;
        inflater = LayoutInflater.from(context);
    }

    public void OnItemClick(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public PictureViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.fragment_picture_item, null);
        PictureViewHolder holder = new PictureViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(PictureViewHolder holder, final int position) {
        Picture picture = pictureList.get(position);

        Glide.with(context).load(picture.getImage_url())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.default_content_pic)
                .error(R.drawable.default_content_pic)
                .crossFade()
                .into(holder.pictureView);

        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.OnItemClick(view, position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return pictureList.size();
    }

    class PictureViewHolder extends XRecyclerView.ViewHolder {
        private ImageView pictureView;

        public PictureViewHolder(View itemView) {
            super(itemView);

            pictureView = (ImageView) itemView.findViewById(R.id.picture_url);
        }
    }

    public interface OnItemClickListener {
        void OnItemClick(View view, int position);
    }
}
